使用 Swagger

介绍

最近项目中需要使用到 Swagger,Swagger 是一个开源软件框架,由大型工具生态系统支持,可帮助开发人员设计,构建,记录和使用 RESTful Web 服务。虽然大多数用户通过 Swagger UI 工具识别 Swagger,但 Swagger 工具集包括对自动文档,代码生成和测试用例生成的支持。

Swagger 组成如下:

  1. Swagger-tools: 提供各种与Swagger进行集成和交互的工具。例如模式检验、Swagger 1.2文档转换成Swagger 2.0文档等功能。
  2. Swagger-core: 用于Java/Scala的的Swagger实现。与JAX-RS(Jersey、Resteasy、CXF…)、Servlets和Play框架进行集成。
  3. Swagger-js: 用于JavaScript的Swagger实现。
  4. Swagger-node-express: Swagger模块,用于node.js的Express web应用框架。
  5. Swagger-ui: 一个无依赖的HTML、JS和CSS集合,可以为Swagger兼容API动态生成优雅文档。
  6. Swagger-codegen: 一个模板驱动引擎,通过分析用户Swagger资源声明以各种语言生成客户端代码。

在 Java 和 Python 中 web 框架都有对 Swagger 的支持,例如 Flask ,Spring Boot 等。但其实我们可以不使用集成的方式,因为 Swagger-ui 本身就是一个独立的项目,我们只需要启动 Swagger-ui 并配置相对应的 json 或者 yml 文件即可。

书写配置文件

可在 http://editor.swagger.io 进行配置,并检测语法的正确性。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
swagger: "2.0"
info:
description: '/?urls.primaryName=DataAPI.json'
version: 1.0.4
title: DataAPI 接口信息
host: '127.0.0.1:8632'
basePath: /api/v1/key/
tags:
- name: 接口组1
description: ""
- name: 接口组2
description: ""
schemes:
- http
paths:
/interface1:
post:
tags:
- 接口组1
summary: interface1
description: ""
operationId: interface1
consumes:
- application/json
produces:
- application/json
parameters:
- name: app_token
in: header
description: interface1 app_token
required: true
type: string
- name: sign
in: header
description: 签名
required: true
type: string
- in: body
name: key
description: ""
required: true
schema:
type: object
properties:
key:
type: object
properties:
product_id:
type: string
cert_id_cipher:
type: string
cert_id:
type: string
required:
- product_id
- cert_id
- cert_id_cipher
responses:
'200':
description: 请求成功

/interface2:
post:
tags:
- 接口组1
summary: interface2
description: ""
operationId: interface2
consumes:
- application/json
produces:
- application/json
parameters:
- name: app_token
in: header
description: interface2 app_token
required: true
type: string
- name: sign
in: header
description: 签名
required: true
type: string
- in: body
name: key
description: ""
required: true
schema:
type: object
properties:
key:
type: object
properties:
member_id:
type: string
product_id:
type: string
trans_date:
type: object
properties:
start:
type: string
end:
type: string
required:
- start
- end
required:
- member_id
- product_id
- trans_date

responses:
'200':
description: 请求成功

/interface3:
post:
tags:
- 接口组2
summary: interface3
description: ""
operationId: interface3
consumes:
- application/json
produces:
- application/json
parameters:
- name: app_token
in: header
description: interface3 app_token
required: true
type: string
- name: sign
in: header
description: 签名
required: true
type: string
- in: body
name: key
description: "xxx"
required: true
schema:
type: object
properties:
key:
type: object
properties:
member_id:
type: string
trans_date:
type: string
ord_id:
type: string
required:
- member_id
- trans_date
responses:
'200':
description: 请求成功

/interface4:
post:
tags:
- 接口组2
summary: interface4
description: ""
operationId: interface4
consumes:
- application/json
produces:
- application/json
parameters:
- name: app_token
in: header
description: interface4 app_token
required: true
type: string
- name: sign
in: header
description: 签名
required: true
type: string
- in: body
name: key
description: "xxxx"
required: true
schema:
type: object
properties:
key:
type: object
properties:
member_id:
type: string
trans_date:
type: string
ord_id:
type: string
required:
- member_id
- trans_date
responses:
'200':
description: 请求成功

/interface5:
post:
tags:
- 接口组2
summary: interface5
description: ""
operationId: interface5
consumes:
- application/json
produces:
- application/json
parameters:
- name: app_token
in: header
description: interface5 app_token
required: true
type: string
- name: sign
in: header
description: 签名
required: true
type: string
- in: body
name: key
description: "xxx"
required: true
schema:
type: object
properties:
key:
type: object
properties:
member_id:
type: string
trans_date:
type: string
ord_id:
type: string
required:
- member_id
- trans_date
responses:
'200':
description: 请求成功

/interface6:
post:
tags:
- 接口组2
summary: interface6
description: ""
operationId: interface6
consumes:
- application/json
produces:
- application/json
parameters:
- name: app_token
in: header
description: interface6 app_token
required: true
type: string
- name: sign
in: header
description: 签名
required: true
type: string
- in: body
name: key
description: "xxx"
required: true
schema:
type: object
properties:
key:
type: object
properties:
member_id:
type: string
trans_date:
type: string
ord_id:
type: string
required:
- member_id
- trans_date
responses:
'200':
description: 请求成功

externalDocs: {
"description": "xxx",
"url": "http://xxx"
}

启动 Swagger

新建一个 swagger.conf 文件
1
2
3
4
5
6
7
8
9
server {
listen 8634;
server_name localhost;

location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
拉取一个 Nginx 镜像
1
docker pull nginx
修改镜像名称为 swagger-nginx
1
docker tag docker.io/nginx:latest swagger-nginx
获取 Swagger UI 代码

https://github.com/swagger-api/swagger-ui 获取最新的 Swagger UI 代码,并将 dist 目录中的内容拷贝到 /app/docker/www/swagger 目录下,方便容器进行映射。

修改读取配置文件的位置

将 dist 下 index.html 中的 url: “https://petstore.swagger.io/v2/swagger.json" 根据实际情况进行修改,若只有一个配置文件,则使用 url 就行(格式不用动)。如果是多个配置文件,则使用 urls,具体格式为 [{url: ““, name: ““},{url: ““, name: ““}] ,详细配置介绍地址为:https://swagger.io/docs/open-source-tools/swagger-ui/usage/configuration/

启动容器
1
docker run -itd -v /app/docker/www/swagger:/usr/share/nginx/html -v /app/docker/www/swagger/swagger.conf:/etc/nginx/conf.d/swagger.conf -p 8634:8634 swagger-nginx bash

可能还需进入容器启动 nginx,nginx -s reload -c 配置文件地址,若提示找不到 pid,直接执行 nginx 生成一个就行。打开浏览器就能看到 Swagger 服务了,并且进行在线调试。

示例 index.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<!-- HTML for static distribution bundle build -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Swagger UI</title>
<link rel="stylesheet" type="text/css" href="./swagger-ui.css" >
<link rel="icon" type="image/png" href="./favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="./favicon-16x16.png" sizes="16x16" />
<style>
html
{
box-sizing: border-box;
overflow: -moz-scrollbars-vertical;
overflow-y: scroll;
}

*,
*:before,
*:after
{
box-sizing: inherit;
}

body
{
margin:0;
background: #fafafa;
}
</style>
</head>

<body>
<div id="swagger-ui"></div>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script src="./swagger-ui-bundle.js"> </script>
<script src="./swagger-ui-standalone-preset.js"> </script>
<script>

window.onload = function() {
// Begin Swagger UI call region
const ui = SwaggerUIBundle({
urls: [{name: "xxx.json", url: "json/xxx.json"}, {name: "xxxx.json", url: "json/xxxx.json"}, {name: "xxxxx.json", url: "json/xxxxx.json"}],
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
})
// End Swagger UI call region

window.ui = ui
}
</script>
</body>
</html>